package com.qf.qflivehou.service.serviceImpl;

import com.qf.qflivehou.config.RedisKey;
import com.qf.qflivehou.mapper.repository.QfTeachRepository;
import com.qf.qflivehou.pojo.respVo.ResponseData;
import com.qf.qflivehou.pojo.vo.QfTeach;
import com.qf.qflivehou.service.QfTeachService;
import org.hibernate.annotations.common.util.impl.LoggerFactory;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author Mr.Liu
 * @date 2022/1/10 22:30
 */
@Service
public class QfTeachServiceImpl implements QfTeachService {
    @Autowired
    private QfTeachRepository qfTeachRepository;
    @Autowired
    private RedisTemplate redisTemplate;

    //声明日志
    private static Logger logger = LoggerFactory.logger(QfTeachServiceImpl.class);

    @Override
    public ResponseData findAll(Integer page,Integer size) {
        //1.判断redis种是否有该key值
        Boolean aBoolean = redisTemplate.hasKey(RedisKey.TEACHER_KEY);
        //2.如果返回值为true,代表有该key值
        if(aBoolean){
            //有该key值,直接从redis中获取
            logger.debug("进入了redis查询");
            int index = (page-1)*size;
            List list = redisTemplate.opsForList().range(RedisKey.TEACHER_KEY, index, index+size-1);
           // System.out.println(list);
            Long size1 = redisTemplate.opsForList().size(RedisKey.TEACHER_KEY);
            return new ResponseData(0,"查询成功",list,size1);
        }else {
            //代表redis中没有该key，则需要从数据库先来进行查询,将结果集放置在Redis中
            logger.debug("进入了数据库查询");
            List<QfTeach> teachers = qfTeachRepository.findAll();
            if(teachers!=null && teachers.size()>0){
                //将查询出的结果集放置在redis中
                redisTemplate.opsForList().leftPushAll(RedisKey.TEACHER_KEY,teachers);
                //从redis中获取
                int index = (page-1)*size;
                List tList = redisTemplate.opsForList().range(RedisKey.TEACHER_KEY, index, index + size - 1);
                Long size1 = redisTemplate.opsForList().size(RedisKey.TEACHER_KEY);
                return new ResponseData(0,"查找成功",tList,size1);
            }else {
                return new ResponseData(0,"查询成功",null,null);
            }
        }
    }
}
